# -*- coding: utf-8 -*-
# @Time    : 2018-05-22 21:02
# @Author  : Yang Feng
# @Email   : yangfeng@bbdservice.com
# @File    : CurrentMaxOverdueMon.py
# @Software: PyCharm

'''eof
name:最大逾期或透支月数
code:CurrentMaxOverdueMon
tableName:
columnName:
groups:个人征信
dependencies:IC
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import json
reload(sys)
sys.setdefaultencoding('utf-8')


def FormatData(tableName):
    try:
        icr = json.loads(IC[0]["icr"])[tableName]
        return icr if isinstance(icr, list) and len(icr) > 0 else [{}]
    except:
        return [{}]


def ConvertDataType(dataValue, dataType):
    returnData = None
    if dataType == 'float':
        try:
            returnData = float(dataValue) if float(dataValue) >= 0 else None
        except:
            returnData = None
    elif dataType == 'None':
        returnData = 0 if dataValue is None else dataValue
    return returnData


def CurrentMaxOverdueMon():
    try:
        # 逾期(透支)信息汇总
        icrOverdueSummary = FormatData('ICROverdueSummary')
        if icrOverdueSummary == [{}]:
            return u'缺失值'
        else:
            monthList = []
            # 贷款逾期月份数
            months = ConvertDataType(icrOverdueSummary[0].get('Months'), 'float')
            # 贷记卡逾期月份数
            months2 = ConvertDataType(icrOverdueSummary[0].get('Months2'), 'float')
            # 准贷记卡60天以上透支月份数
            months3 = ConvertDataType(icrOverdueSummary[0].get('Months3'), 'float')

            if months is None and months2 is None and months3 is None:
                return u'缺失值'
            monthList.append(ConvertDataType(months, 'None'))
            monthList.append(ConvertDataType(months2, 'None'))
            monthList.append(ConvertDataType(months3, 'None'))
            return max(monthList)
    except:
        return u'缺失值'


result = CurrentMaxOverdueMon()